
import numpy as np


# 02

dt_int64 = np.dtype(np.int64)
print(dt_int64)
# int64

dt_obj = np.dtype([('name', np.str_)])
print(dt_obj)
dt_age = np.dtype([('age', np.int_)])
print(dt_age)
dt_score = np.dtype([('score', np.float32)])
print(dt_score)
# # [('name', '<U')]
# # [('age', '<i8')]
# # [('score', '<f4')]

stu = np.dtype([('name', 'U3'), ('age', 'i4'), ('score', 'f4')])
print(stu)
# [('name', '<U3'), ('age', '<i4'), ('score', '<f4')]

stu_list = np.array([('Tom', 21, 60), ('Yan', 20, 59)], dtype=stu)
print(stu_list)
# [('Tom', 21, 60.) ('Yan', 20, 59.)]


def create_rgba_dtype() -> np.ndarray:
    color = np.dtype([('r', np.ubyte, 1),
                      ('g', np.ubyte, 1),
                      ('b', np.ubyte, 1),
                      ('a', np.ubyte, 1)])
    c = np.array((255, 255, 255, 1), dtype=color)
    return c


print(create_rgba_dtype())
# (255, 255, 255, 1)

# 03
x = np.empty([2, 2], dtype=int)
print(x)
# [[43221616        0]
#  [       0        0]]

a = np.zeros(4)
print(a)
# [0. 0. 0. 0.]
b = np.zeros((2, 3), dtype=np.float32)
print(b)
# [[0. 0. 0.]
#  [0. 0. 0.]]
b = np.zeros([2, 3], dtype=np.float)
print(b)
# [[0. 0. 0.]
#  [0. 0. 0.]]
c = np.zeros((2, 2), dtype=[('x', 'i4'), ('y', 'f4')])
print(c)
# [[(0, 0.) (0, 0.)]
#  [(0, 0.) (0, 0.)]]

c = np.zeros(2, dtype=[('x', 'i8'), ('y', 'f8')])
print(c)
# [(0, 0.) (0, 0.)]

a = np.ones(5)
print(a)
# [1. 1. 1. 1. 1.]

b = np.ones((4, 2), dtype=np.int32)
print(b)
# [[1 1]
#  [1 1]
#  [1 1]
#  [1 1]]

temp = np.zeros((2, 2))
c = np.ones_like(temp)
print(c)
# [[1. 1.]
#  [1. 1.]]

a = np.arange(5)
print(a)
# [0 1 2 3 4]
print(type(a))
# <class 'numpy.ndarray'>

b = np.arange(1, 5)
print(b)
# [1 2 3 4]

c = np.arange(1, 5, 2)
print(c)
# [1 3]

d = np.arange(1, 5.2, 0.6)
print(d)
# [1.  1.6 2.2 2.8 3.4 4.  4.6 5.2]


# 04
def create_matrix() -> np.ndarray:
    matrix = np.zeros([8, 8], dtype=np.int)
    for i_i, i_v in enumerate(matrix):
        for j_i, j_v in enumerate(i_v):
            if (i_i + j_i) % 2 != 0:
                matrix[i_i][j_i] = 1
    return matrix


print(create_matrix())
# [[0 1 0 1 0 1 0 1]
#  [1 0 1 0 1 0 1 0]
#  [0 1 0 1 0 1 0 1]
#  [1 0 1 0 1 0 1 0]
#  [0 1 0 1 0 1 0 1]
#  [1 0 1 0 1 0 1 0]
#  [0 1 0 1 0 1 0 1]
#  [1 0 1 0 1 0 1 0]]

# 05
a = [1, 2, 3, 4]
new_arr = np.asarray(a)
print(new_arr)
# [1 2 3 4]

new_arr = np.asarray(a, dtype=np.float32)
print(new_arr)
# [1. 2. 3. 4.]

# buffer 是字符串的时候，Python3 默认 str 是 Unicode 类型，所以要转成 bytestring 在原 str 前加上 b
s = b'Hello World'
a = np.frombuffer(s, dtype='S1')
print(a)
# [b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']

from numpy import random

a = random.randint(100)
print(a)
print(type(a))
# 59
# <class 'int'>

a = random.randint(100, size=5)
print(a)
# [74 36 11 21 87]

b = random.rand(3, 5)
print(b)
# [[0.8880049  0.60627652 0.70053748 0.32913766 0.39218565]
#  [0.72646841 0.79181732 0.54927297 0.42795332 0.14081282]
#  [0.98553459 0.67325698 0.69503988 0.10474299 0.7934749 ]]

# 06

x = np.arange(10)
arr_cut = slice(2, 8, 3)
print(arr_cut, type(slice))
print(x[arr_cut], type(x[arr_cut]))
# slice(2, 8, 3) <class 'type'>
# [2 5] <class 'numpy.ndarray'>

y = x[2:8:3]
print(y, type(y))
# [2 5] <class 'numpy.ndarray'>

# todo -1?
x = np.arange(1, 10).reshape(3, -1)
print(x)
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]

# todo
y = x[:2, :2]
print(y)
# [[1 2]
#  [4 5]]

y[1, 1] = 999
print(y)
# [[  1   2]
#  [  4 999]]
print(x)
# [[  1   2   3]
#  [  4 999   6]
#  [  7   8   9]]

x = np.arange(10, 1, -1)
print(x)
# [10  9  8  7  6  5  4  3  2]

y = x[np.array([3, 3, 1, 8])]
print(y)
# [7 7 9 2]

